//
//  SISSContractTwoMiddleFillInCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/9/30.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractTextFillInCell.h"

@interface SISSContractTextFillInCell()
/// label
@property (weak, nonatomic) YYLabel *label;

/// textFieldArrM
@property (strong, nonatomic) NSMutableArray *textFieldArrM;

@property (strong, nonatomic) NSMutableArray *arrM;

@end
@implementation SISSContractTextFillInCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
//    self.bottomLine.hidden = contractModel.isHiddenBottomLine;
    if (!contractModel.strArr) return;
    if (self.textFieldArrM.count > 0) [self.textFieldArrM removeAllObjects];
    for (NSInteger i = 1; i < contractModel.strArr.count; i++) {
        UITextField *textField = [[UITextField alloc] init];
        textField.text = contractModel.array ? [NSString stringWithFormat:@"%@",contractModel.array[i - 1]] : @"";
        [self.textFieldArrM addObject:textField];
    }
    self.arrM = [NSMutableArray arrayWithCapacity:self.textFieldArrM.count];
    for (NSInteger i = 0; i < self.textFieldArrM.count; i++) {
        [self.arrM addObject:@""];
    }
    NSMutableArray *strArrM = [NSMutableArray arrayWithArray:contractModel.strArr];
    [strArrM removeObjectAtIndex:0];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:contractModel.strArr.firstObject attributes:nil];
    
    NSMutableAttributedString *attr = [self getTextAttritedStrWithAttachText:attributedText strs:strArrM textFields:self.textFieldArrM keyboardType:[SISSPublicMethods matchKeybordTypeWithKeyboardNum:contractModel.keyboardNum > 0 ? contractModel.keyboardNum : 9] placeHolders:contractModel.placeHolders];
    if ([[contractModel.strArr.firstObject substringToIndex:1] isEqualToString:@"*"]) {
        [attr setAttributes:@{
                              NSForegroundColorAttributeName : [UIColor redColor],
                              NSFontAttributeName : [UIFont systemFontOfSize:16]
                              } range:NSMakeRange(0, 1)];
        
    }
    self.label.attributedText = attr;
    if (contractModel.placeHolder.length > 0) {
        UITextField *textField = self.textFieldArrM.firstObject;
        textField.keyboardType = UIKeyboardTypeDefault;
    }
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
        
    for (NSInteger i = 0; i < self.textFieldArrM.count; i++) {
        if (textField == self.textFieldArrM[i]) {
            [self.arrM replaceObjectAtIndex:i withObject:textField.text];
        }
    }
    self.contractModel.array = self.arrM.copy;
}

#pragma mark - UI
- (void)initUI {
    [super initUI];

    YYLabel *label = [[YYLabel alloc] init];
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = (SCREEN_WIDTH - 32);
    label.userInteractionEnabled= YES;
    [self.contentView addSubview:label];
    self.label = label;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.label.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}

- (NSMutableArray *)textFieldArrM {
    if (!_textFieldArrM) {
        _textFieldArrM = [NSMutableArray array];
    }
    return _textFieldArrM;
}


@end
